import {Application, FrontendSession} from 'pinus';
import {logger} from "../../../util/logger";
import UserDao from "../../../dao/mysqlDao/userDao";

export default function (app: Application) {
    return new Handler(app);
}

export class Handler {

    constructor(private app: Application) {
        this.app = app;
    }


    /**
     * New client entry.
     *
     * @param  {Object}   msg     request message
     * @param  {Object}   session current session object
     * @param  {Function} next    next step callback
     * @return {Void}
     */
    public async entry(msg: any, session: FrontendSession) {

        // let data  = {
        //     account:"1",
        //     nickname: "昵称aaa"
        // };
        //
        // let reData = await this.app.rpc.area.areaRemote.createRole(session, data);


        // 根据uid去获取user表 角色基本信息
        let [errCode2, userData] = await UserDao.getUsersByUid(0);
        if (errCode2) {
            logger.info("errCode2 --> ", errCode2);
            return {code: 500, msg: "GET_USER_ERRO"};
        }

        let clientAddress = this.app.get('sessionService').getClientAddressBySessionId(session.id);
        let IP = clientAddress.ip.split('::ffff:')[1];

        console.log("userData[0] -> " , userData[0]);
        // rpc到area服，通知玩家已经执行了登录操作 并返回对应所需信息
        let loginInfo = await this.app.rpc.area.areaRemote.login(session, {userInfo:userData[0],IP:IP});
        console.log("loginInfo ----->>>>>>   " , loginInfo);

        return {code: 200, msg: 'game server is ok.'};
    }


}